package com.tengju.user.infrastructure.repository.convert;

import com.tengju.data.interfaces.api.vo.StarChangeVO;
import com.tengju.data.interfaces.api.vo.StarIndicatorStatVO;
import com.tengju.user.domain.model.sign.*;
import com.tengju.user.domain.model.user.Staff;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.DateUtil;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.*;

import java.util.Date;

public class StarSignConvert {

    private StarSignConvert() {
    }

    public static StarSignInfo signInfoDOToInfo(StarSignInfoDO infoDO) {
        if (infoDO == null) {
            return null;
        }
        return new StarSignInfo(
                SignId.of(infoDO.getId()),
                UserIdCode.of(infoDO.getIdCode()),
                infoDO.getBeforeStarRole(),
                infoDO.getBeforeStarLevel(),
                infoDO.getStarRole(),
                infoDO.getStarLevel(),
                infoDO.getTriggerType(),
                DateUtil.dateToLocalDateTime(infoDO.getInviteDate()),
                infoDO.getAccountType(),
                infoDO.getStep(),
                DateUtil.dateToLocalDateTime(infoDO.getAuditTime()),
                infoDO.getIndicatorRequireStat(),
                infoDO.getAuditStat(),
                infoDO.getAuditDesc(),
                DateUtil.dateToLocalDateTime(infoDO.getExpireDate()),
                infoDO.getInvalidDesc(),
                infoDO.getChangeRecord(),
                infoDO.getCreateBy(),
                infoDO.getAuditBy(),
                infoDO.getUpdateBy()
        );
    }

    public static StarContractInfo convertDOToEntity(StarContractInfoDO starContractInfoDO) {
        if (starContractInfoDO == null) {
            return null;
        }
        return new StarContractInfo(
                UserIdCode.of(starContractInfoDO.getIdCode()),
                starContractInfoDO.getContractType(),
                starContractInfoDO.getContractId(),
                starContractInfoDO.getContractStatus().intValue(),
                DateUtil.dateToLocalDateTime(starContractInfoDO.getContractSignDate()),
                DateUtil.dateToLocalDateTime(starContractInfoDO.getContractExpireDate())
        );
    }

    public static StarSignInfoDO entityToDO(StarSignInfo starSignInfo, Date createTime,
            Date updateTime) {
        StarSignInfoDO starSignInfoDO = new StarSignInfoDO();
        starSignInfoDO.setId(starSignInfo.getId() == null ? null : starSignInfo.getId().getValue());
        starSignInfoDO.setIdCode(starSignInfo.getIdCode().getValue());
        starSignInfoDO.setBeforeStarRole(starSignInfo.getBeforeStarRole());
        starSignInfoDO.setBeforeStarLevel(starSignInfo.getBeforeStarLevel());
        starSignInfoDO.setStarRole(starSignInfo.getStarRole());
        starSignInfoDO.setStarLevel(starSignInfo.getStarLevel());
        starSignInfoDO.setTriggerType(starSignInfo.getTriggerType());
        starSignInfoDO.setInviteDate(DateUtil.localDateTimeToDate(starSignInfo.getInviteDate()));
        starSignInfoDO.setAccountType(starSignInfo.getAccountType());
        starSignInfoDO.setStep(starSignInfo.getStep());
        starSignInfoDO.setAuditTime(DateUtil.localDateTimeToDate(starSignInfo.getAuditTime()));
        starSignInfoDO.setIndicatorRequireStat(starSignInfo.getIndicatorRequireStat());
        starSignInfoDO.setAuditDesc(starSignInfo.getAuditDesc());
        starSignInfoDO.setAuditStat(starSignInfo.getAuditStat());
        starSignInfoDO.setExpireDate(DateUtil.localDateTimeToDate(starSignInfo.getExpireDate()));
        starSignInfoDO.setInvalidDesc(starSignInfo.getInvalidDesc());
        starSignInfoDO.setChangeRecord(starSignInfo.getChangeRecord());
        starSignInfoDO.setCreateBy(starSignInfo.getCreateBy());
        starSignInfoDO.setAuditBy(starSignInfo.getAuditBy());
        starSignInfoDO.setUpdateBy(starSignInfo.getUpdateBy());
        starSignInfoDO.setCreateTime(createTime);
        starSignInfoDO.setUpdateTime(updateTime);
        return starSignInfoDO;
    }


    public static StarLimitInfo blackLimitToStarLimit(StarBlacklistDO starBlacklistDO) {
        return new StarLimitInfo(
                UserIdCode.of(starBlacklistDO.getIdCode()),
                starBlacklistDO.getLimitHighestStarLevel(),
                starBlacklistDO.getRemark(),
                starBlacklistDO.getValidStatus(),
                DateUtil.dateToLocalDateTime(starBlacklistDO.getCreateTime())
        );
    }

    public static StarConfIndicator configDOToStarConfIndicator(CommonConfigDataDO configDataDO) {
        return new StarConfIndicator(
                configDataDO.getConfKey(),
                configDataDO.getConfKeyDesc(),
                configDataDO.getConfValue()
        );
    }

    public static StarIndicatorStat starIndicatorToEntity(StarIndicatorStatVO starIndicatorStatVO) {
        return new StarIndicatorStat(
                UserIdCode.of(starIndicatorStatVO.getIdCode()),
                starIndicatorStatVO.getIndicatorName(),
                starIndicatorStatVO.getIndicatorCode(),
                starIndicatorStatVO.getIndicatorValue(),
                starIndicatorStatVO.getIndicatorDay()
        );
    }

    public static StarIndicatorStat starIndicatorToEntity(StarIndicatorStatDO starIndicatorStatDO) {
        return new StarIndicatorStat(
                UserIdCode.of(starIndicatorStatDO.getIdCode()),
                starIndicatorStatDO.getIndicatorName(),
                starIndicatorStatDO.getIndicatorCode(),
                starIndicatorStatDO.getIndicatorValue(),
                starIndicatorStatDO.getIndicatorDay()
        );
    }

    public static StarContractInfoDO convertEntityToDO(StarContractInfo contractInfo) {
        Date now = new Date();
        StarContractInfoDO starContractInfoDO = new StarContractInfoDO();
        starContractInfoDO.setIdCode(contractInfo.getIdCode().getValue());
        starContractInfoDO.setContractType(contractInfo.getContractType());
        starContractInfoDO.setContractId(contractInfo.getContractId());
        starContractInfoDO.setContractStatus(contractInfo.getContractStatus().byteValue());
        starContractInfoDO.setContractSignDate(
                DateUtil.localDateTimeToDate(contractInfo.getContractSignDate()));
        starContractInfoDO.setContractExpireDate(
                DateUtil.localDateTimeToDate(contractInfo.getContractExpireDate()));
        starContractInfoDO.setCreateTime(now);
        starContractInfoDO.setUpdateTime(now);
        return starContractInfoDO;
    }

    public static StarChangeNoticeRecord noticeRecordDOToEntity(StarChangeNoticeRecordDO starChangeVO) {
        return new StarChangeNoticeRecord(
                UserIdCode.of(starChangeVO.getIdCode()),
                starChangeVO.getBeforeStarRole(),
                starChangeVO.getBeforeStarLevel(),
                starChangeVO.getTargetStarRole(),
                starChangeVO.getTargetStarLevel(),
                StarChangeNoticeTypeEnum.getByType(starChangeVO.getChangeType()),
                starChangeVO.getNoticeDate()
        );
    }

    public static StarBlacklistDO starLimitToDO(StarLimitInfo limitInfo, Staff staff) {
        StarBlacklistDO starBlacklistDO = new StarBlacklistDO();
        starBlacklistDO.setIdCode(limitInfo.getIdCode().getValue());
        starBlacklistDO.setLimitHighestStarLevel(limitInfo.getLimitHighestStarLevel());
        starBlacklistDO.setValidStatus(StarBlacklistValidStatusEnum.VALID.getCode());
        starBlacklistDO.setRemark(limitInfo.getRemark());
        starBlacklistDO.setCreateBy(staff.getName());
        starBlacklistDO.setUpdateBy(staff.getName());
        return starBlacklistDO;
    }


    public static SignStatisticsDetail SignStatisticsDetailDOToModel(SignStatisticsDetailDO signStatisticsDetailDO) {
        return new SignStatisticsDetail(
                UserIdCode.of(signStatisticsDetailDO.getIdCode()),
                StarLevelEnum.parseByCode(signStatisticsDetailDO.getStarLevel()),
                StarSignStepEnum.getByStep(signStatisticsDetailDO.getSignStep()),
                signStatisticsDetailDO.getAccountType() == null ? null : StarAccountTypeEnum.getByType(signStatisticsDetailDO.getAccountType()),
                signStatisticsDetailDO.getLastMonthMoneyOut(),
                signStatisticsDetailDO.getThisMonthMoneyOut(),
                signStatisticsDetailDO.getInviteTime()
        );
    }

//    public static IdentityVO signResToVo (SignRes signRes) {
//
//        IdentityVO identityVO = new IdentityVO();
//        identityVO.setIdentityType(signRes.getUserFrom());
//        identityVO.setAuthTime(signRes.getSignTime());
//        return identityVO;
//
//    }

}
